//
//#include "graphics/rendervisitor.h"
//#include "graphics/graphicsmanager.h"
//#include "graphics/materialmanager.h"
//#include "graphics/material.h"
//#include "graphics/mesh.h"
//#include "graphics/submesh.h"
//#include "graphics/renderer.h"
//
//namespace dw
//{
//	namespace graphics
//	{
//
//		RenderVisitor::RenderVisitor() : _showHidden(false), _numNodes(0), _numNodeTotal(0)
//		{
//		}
//
//		bool RenderVisitor::visit(Node* visited)
//		{
//			if ((_showHidden || !visited->isHidden()) && visited->hasObject())
//			{
//				Object* object = visited->getObject();
//				if (object->getType() == Object::MESH)
//				{	
//					_numNodeTotal++;
//
//					Mesh* mesh						= static_cast< Mesh* >(object);
//					
//					
//					//Cuboid xbb = mesh->getBoundingBox();
//					//xbb *= visited->getTransform(Node::CS_WORLD);
//
//					//GraphicsManager::getInstance().getRenderer().pushMatrix();
//					//GraphicsManager::getInstance().getRenderer().multMatrix(visited->getTransform(Node::CS_WORLD));
//					//GraphicsManager::getInstance().getRenderer().drawBox(xbb);
//					//GraphicsManager::getInstance().getRenderer().popMatrix();
//					
//					//xbb *= Matrix44f::inverse(_cameraNode->getTransform(Node::CS_WORLD));
//
//					//if (_frustum->contains(xbb))
//					{
//						_numNodes++;
//						for (unsigned i = 0; i < mesh->getNumSubMeshes(); ++i)
//						{
//							SubMesh* subMesh	= mesh->getSubMesh(i);
//							Batch batch;
//
//							/*if (visited->hasAnimation())
//								batch.frame = visited->getAnimation()->frame(_dt).toMatrix();
//							else*/
//							batch.xform		= visited->getTransform();
//							batch.vb			= mesh->getVertexBuffer();
//							batch.ib			= mesh->getIndexBuffer();
//							batch.primitiveType = subMesh->getPrimitiveType();
//							batch.indexStart		= subMesh->getIndexStart();
//							batch.indexCount		= subMesh->getIndexCount();
//							batch.minIndex			= subMesh->getVertexIndexStart();
//							batch.maxIndex			= subMesh->getVertexIndexEnd();
//							batch.primitiveType = subMesh->getPrimitiveType();
//							batch.material			=	MaterialManager::getInstance().getMaterial(subMesh->getMaterialName());
//							batch.color					=	visited->getColor();
//
//
//							//float cameraDistance	= (visited->getPosition() - _cameraNode->getPosition()).length();
//
//							std::string materialName;
//							if (batch.material)
//								materialName = batch.material->getName();
//
//							if (batch.material && (batch.material->getOpacity() < 1.0f))
//								_renderBatchesByTransparentMaterial.insert(std::make_pair(materialName, batch));
//							else
//								_renderBatchesByMaterial.insert(std::make_pair(materialName, batch));
//
//							//_renderBatchesZSort.insert(std::make_pair(cameraDistance, batch));
//						}
//						return true;
//					}
//				}
//			}
//			return false;
//		}
//
//		void RenderVisitor::render(Renderer* renderer)
//		{
//			BatchMaterialMap::iterator iter;
//
//			for (iter = _renderBatchesByMaterial.begin(); iter != _renderBatchesByMaterial.end(); ++iter)
//			{
//				Batch batch = iter->second;
//
//				if (batch.material)
//				{
//					glDisable(GL_COLOR_MATERIAL);
//					//renderer->setMaterial(batch.material);
//				}
//				else
//				{
//					glEnable(GL_COLOR_MATERIAL);
//				}
//				renderer->setColor(batch.color);
//				renderer->setVertexBuffer(batch.vb);
//				renderer->setIndexBuffer(batch.ib);
//				renderer->pushMatrix();
//				renderer->multMatrix(batch.xform);
//				renderer->drawIndexedPrimitive(batch.primitiveType, 
//					batch.indexStart, 
//					batch.indexCount, 
//					batch.minIndex, 
//					batch.maxIndex);
//				renderer->popMatrix();
//			}
//			/*glDisable(GL_LIGHTING);
//			glEnable(GL_BLEND);
//			//glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//			glBlendFunc (GL_ONE, GL_ONE);*/
//			for (iter = _renderBatchesByTransparentMaterial.begin(); iter != _renderBatchesByTransparentMaterial.end(); ++iter)
//			{
//				//renderer->setMaterial(iter->second.material);
//				renderer->setVertexBuffer(iter->second.vb);
//				renderer->setIndexBuffer(iter->second.ib);
//				renderer->pushMatrix();
//				renderer->multMatrix(iter->second.xform);
//				renderer->drawIndexedPrimitive(iter->second.primitiveType, 
//					iter->second.indexStart, 
//					iter->second.indexCount, 
//					iter->second.minIndex, 
//					iter->second.maxIndex);
//				renderer->popMatrix();
//			}
//			/*glDisable(GL_BLEND);
//			glEnable(GL_LIGHTING);*/
//		}
//
//	} // namespace graphics
//} // namespace dw
